package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.exception.CategoryDeleteFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        // 获取当前时间作为创建时间和更新时间
//        LocalDateTime now = LocalDateTime.now();
        // 获取用户id作为创建人id和更新人id
//        Long currentId = BaseContext.getCurrentId();

        Category category = Category.builder()
                .status(StatusConstant.DISABLE)
//                .createTime(now)
//                .updateTime(now)
//                .createUser(currentId)
//                .updateUser(currentId)
                .build();

        // 同名属性赋值
        BeanUtils.copyProperties(categoryDTO, category);

        categoryMapper.addCategory(category);
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 获取分页信息
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        // 分页插件
        PageHelper.startPage(page, pageSize);
        List<Category> categories = categoryMapper.page(categoryPageQueryDTO);
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
        // 获取数量和当前页分类
        long total = categoryPageInfo.getTotal();
        List<Category> list = categoryPageInfo.getList();
        // 封装结果
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(list);

        return pageResult;
    }

    /**
     * 启售/停售套餐
     * @param status
     * @param id
     */
    @Override
    public void enableOrDisable(Integer status, Long id) {
        // 获取更新时间
//        LocalDateTime updateTIme = LocalDateTime.now();
        // 获取更新人id
//        Long updateUser = BaseContext.getCurrentId();

        Category category = Category.builder()
                                    .id(id)
                                    .status(status)
//                                    .updateTime(updateTIme)
//                                    .updateUser(updateUser)
                                    .build();
        categoryMapper.update(category);
    }

    /**
     * 修改分类信息
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        // 获取更新时间
//        LocalDateTime updateTime = LocalDateTime.now();
        // 获取更新人id
//        Long updateUser = BaseContext.getCurrentId();
        Category category = Category.builder()
//                                    .updateTime(updateTime)
//                                    .updateUser(updateUser)
                                    .build();

        // 同名属性赋值
        BeanUtils.copyProperties(categoryDTO, category);

        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void delete(Long id) {
        if (dishMapper.countByCategoryId(id) > 0){
            throw new CategoryDeleteFailedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if (setmealMapper.countByCategoryId(id) > 0){
            throw new CategoryDeleteFailedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        List<Category> categories = categoryMapper.getBytype(type);
        return categories;
    }

    /**
     * 查询所有分类信息
     * @return
     */
    @Override
    public List<Category> getCategory() {
        List<Category> categories = categoryMapper.getCategory();
        return categories;
    }
}
